import {createRouter, createWebHistory} from 'vue-router'
import FrontLayout from "@/views/layout/FrontLayout.vue";
import AdminLayout from "@/views/layout/AdminLayout.vue";
import tools from "@/utils/tools.js";



const router = createRouter({
    history: createWebHistory(),
    routes: getRoutes()
})

// Global navigation guard
router.beforeEach((to, from, next) => {
    // Public routes that don't require authentication
    const publicRoutes = ['/index', '/login', '/register', '/'];
    
    // Check if the user is trying to access a protected route
    if (!publicRoutes.includes(to.path) && !tools.getToken()) {
        // User is not authenticated and trying to access a protected route
        console.log(`Unauthorized access to ${to.path}, redirecting to login`);
        // Redirect to login with the intended destination stored in the query parameters
        next({
            path: '/login',
            query: { redirect: to.fullPath } // Save the full path to return after login
        });
    } else {
        // User is authenticated or accessing a public route
        next();
    }
});



function getRoutes() {
    let defaultRoutes = [
        {
            path: '/',
            name: 'front',
            component: FrontLayout,
            redirect: "/index",
            children: [{
                path: "index",
                name: "index",
                component: () =>
                  import ('../views/front/Index.vue')
            }, {
                path: "editPassword",
                name: "front-editPassword",
                component: () =>
                  import ('../views/EditPassword.vue')
            }, {
                path: 'productList',
                name: 'front-productList',
                component: () => import('../views/front/ProductList.vue')
            }, {
                path: 'productDetails/:id',
                name: 'front-productDetails',
                component: () => import('../views/front/ProductDetails.vue')
            },
                {
                    path: 'productOrder',
                    name: 'front-ProductOrder',
                    component: () => import('../views/front/ProductOrder.vue')
                },

                {
                    path: 'shoppingCart',
                    name: 'front-shoppingCart',
                    component: () => import('../views/front/ShoppingCart.vue')
                },

                {
                    path: 'personalCenter',
                    name: 'front-personalCenter',
                    component: () => import('../views/front/PersonalCenter.vue')
                },
                {
                    path: 'payment/:orderId',
                    name: 'front-payment',
                    component: () => import('../views/front/Payment.vue')
                },
            ]
        },
        {
            path: '/admin',
            name: 'admin',
            component: AdminLayout,
            redirect: "/admin/home",
            children: [{
                path: "home",
                name: "admin-home",
                component: () =>
                    import ('../views/admin/Home.vue')
            },
                {
                    path: 'editPassword',
                    name: 'admin-editPassword',
                    component: () =>
                      import ('../views/EditPassword.vue')
                },
                {
                    path: 'admin',
                    name: 'Admin',
                    component: () =>
                      import ('../views/admin/AdminManage.vue')
                },
                {
                    path: 'productType',
                    name: 'admin-productType',
                    component: () =>
                      import ('../views/admin/ProductTypeManage.vue')
                }
                ,
                {
                    path: 'product',
                    name: 'admin-product',
                    component: () =>
                      import ('../views/admin/ProductManage.vue')
                }
                ,
                {
                    path: 'productMainType',
                    name: 'admin-productMainType',
                    component: () =>
                      import ('../views/admin/ProductMainTypeManage.vue')
                }
                ,
                {
                    path: 'shippingAddress',
                    name: 'admin-shippingAddress',
                    component: () =>
                      import ('../views/admin/ShippingAddressManage.vue')
                }
                ,
                {
                    path: 'productOrder',
                    name: 'admin-productOrder',
                    component: () =>
                      import ('../views/admin/ProductOrderManage.vue')
                }
                ,
                {
                    path: 'productOrderEvaluate',
                    name: 'admin-productOrderEvaluate',
                    component: () =>
                      import ('../views/admin/ProductOrderEvaluateManage.vue')
                }
                ,
                {
                    path: 'advertising',
                    name: 'admin-advertising',
                    component: () =>
                      import ('../views/admin/AdvertisingManage.vue')
                }
                ,
                {
                    path: 'slideshow',
                    name: 'admin-slideshow',
                    component: () =>
                      import ('../views/admin/SlideshowManage.vue')
                }


            ]
        },
        {
            path: "/login",
            name: "login",
            component: () =>
              import ('../views/Login.vue')
        }, {
            path: "/register",
            name: "register",
            component: () =>
              import ('../views/Register.vue')
        }];
    defaultRoutes.push({
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        meta: {
            name: ''
        },
        component: () => import ('../views/404.vue')
    })
    console.log('getDynamicRoutes', defaultRoutes)
    return defaultRoutes;
}

export default router
